package com.zzg.srb.oss.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.PutObjectRequest;
import com.zzg.srb.common.exception.BusinessException;
import com.zzg.srb.common.result.ResponseEnum;
import com.zzg.srb.oss.service.FileService;
import com.zzg.srb.oss.utils.OssProperties;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.UUID;

/**
 * @Author zhengzg
 * @Date 2022/6/3 10:16
 * @Version 1.0
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Override
    public String upload(InputStream inputStream, String module, String fileName) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.ACCESSKEYID, OssProperties.SECRET);

        try {

            // 判断BUCKET是否存在
            if (!ossClient.doesBucketExist(OssProperties.BUCKET)){
                ossClient.createBucket(OssProperties.BUCKET);
                ossClient.setBucketAcl(OssProperties.BUCKET, CannedAccessControlList.PublicRead);
            }

            //创建文件按名
            String timeFolder = new DateTime().toString("yyyy/MM/dd");
            //文件名生成
            fileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));

            //组装路径
            String key = module + "/" + timeFolder +"/" + fileName;

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKET, key, inputStream);
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            ossClient.putObject(putObjectRequest);

            //文件url地址
            return "https://" + OssProperties.BUCKET + "." + OssProperties.ENDPOINT + "/" + key;
        } catch (OSSException | ClientException oe) {
            throw new BusinessException(ResponseEnum.ERROR,oe);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void removeFile(String url) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.ACCESSKEYID, OssProperties.SECRET);

        try {

            String host = "https://" + OssProperties.BUCKET + "." + OssProperties.ENDPOINT + "/";
            String objectName = url.substring(host.length());

            ossClient.deleteObject(OssProperties.BUCKET,objectName);
        }catch (OSSException | ClientException oe) {
            throw new BusinessException(ResponseEnum.ERROR,oe);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }
}
